from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# from .models import User
# from mall.apps.users.models import User

# from apps.users.models import User # 运算报错 多了apps

from users.models import User

# Create your views here.

# APIView
# GenericAPIView
# ListAPIView , CreateAPIView

class RegisterUserNameView(APIView):

    """                               需要用序列化器校验
    GET     /users/usernames/(?P<username>\w{5,20})/count/

    GET     /users/username/?username=xxx
    POST    /users/usernames/     username=xxx


    """

    def get(self,request,username):
        # 查询数据库  看有没有制定的数据

        count = User.objects.filter(username=username).count()
        # count = 0 不存在
        # count >=1   存在

        context = {
            'count':count,
            'username':username   # 想返回就返回,不返回 他要数据在返回
        }

        return  Response(context)





